package com.xiaoyu.lanling.view;

import android.graphics.Paint;
import android.os.Parcelable;
import android.util.SparseArray;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import in.srain.cube.util.CLog;

public class FragmentAdapter extends FragmentStatePagerAdapter {

    private final static String TAG = "FragmentAdapter";

    public FragmentManager mFragmentManager;
    private List<? extends OnShowFragment> mThisFragments;
    private List<CharSequence> mTitles;
    private SparseArray<OnShowFragment> instantiateFragments = new SparseArray<>();

    public FragmentAdapter(FragmentManager fm, List<? extends OnShowFragment> thisFragments, List<CharSequence> titles) {
        super(fm);
        mFragmentManager = fm;
        mThisFragments = thisFragments;
        mTitles = titles;
    }

    @Override
    public OnShowFragment getItem(int position) {
        OnShowFragment fragment = mThisFragments.get(position);
        CLog.d(TAG, "getItem: %s %s", position, fragment);
        return fragment;
    }

    public OnShowFragment getFragment(int position) {
        if (instantiateFragments == null || instantiateFragments.size() <= position) {
            return null;
        }
        return instantiateFragments.get(position);
    }

    public void tryShowOnIndex(int index) {
        OnShowFragment fragment = getFragment(index);
        if (fragment != null) {
            fragment.onShow();
        }
    }

    public void tryHideOnOthersIndex(int position) {
        if (instantiateFragments == null || instantiateFragments.size() <= position) {
            return;
        }

        for (int i = 0; i < instantiateFragments.size(); i++) {
            OnShowFragment fragment = instantiateFragments.valueAt(i);
            if (fragment != null && position != i) {
                fragment.onHide();
            }
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        if (obj instanceof OnShowFragment) {
            OnShowFragment fragment = (OnShowFragment) obj;
            instantiateFragments.put(position, fragment);
        }
        return obj;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        super.restoreState(state, loader);
        try {
            TextView tv = null;
            tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            instantiateFragments.clear();
            Field privateStringField = null;
            privateStringField = FragmentStatePagerAdapter.class.getDeclaredField("mFragments");
            privateStringField.setAccessible(true);
            final ArrayList<Object> fragments = (ArrayList<Object>) privateStringField.get(this);
            for (int i = 0; i < fragments.size(); i++) {
                instantiateFragments.put(i, (OnShowFragment) fragments.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        instantiateFragments.remove(position);
    }

    @Override
    public int getCount() {
        return mThisFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }
}
